package com.leaguefox.web.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.leaguefox.web.client.LeagueFoxServiceAsync;
import com.leaguefox.web.client.common.TableCellRenderer;
import com.leaguefox.web.shared.domain.dto.PlayerDto;

public class PlayersViewImpl extends Composite implements PlayersView<PlayerDto> {
	EventHandler eventHandler;

	@UiTemplate("PlayersView.ui.xml")
	interface MyUiBinder extends UiBinder<Widget, PlayersViewImpl> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	FlexTable playersTable;

	@UiField
	Button addPlayerButton;

	@UiField
	Button refreshPlayersButton;

	private LeagueFoxServiceAsync service;

	private Presenter<PlayerDto> presenter;

	private TableCellRenderer<PlayerDto> tableCellRenderer;

	public PlayersViewImpl() {
		// sets listBox
		initWidget(uiBinder.createAndBindUi(this));
		/*
		 * for (String name : names) { listBox.addItem(name); }
		 */
	}

	@UiHandler("refreshPlayersButton")
	void handleClick(ClickEvent e) {
		this.service.getAllPlayers(new AsyncCallback<ArrayList<PlayerDto>>() {

			@Override
			public void onSuccess(ArrayList<PlayerDto> players) {
				// Window.alert("Size: " + arg0.size());
				updatePlayersTable(players);
			}

			@Override
			public void onFailure(Throwable arg0) {
				arg0.printStackTrace();
				Window.alert("Exception1: " + arg0.getMessage());
			}
		});

	}

	private void updatePlayersTable(List<PlayerDto> players) {
		try {
			System.out.println("Players size: " + players.size());

			// Window.alert("updating table");
			this.playersTable.clear();
			this.playersTable.setBorderWidth(1);
			this.playersTable.setTitle("All players");
			
			this.playersTable.setText(0, 0, "Check"); 
			this.playersTable.setText(0, 1, "Name");
			this.playersTable.setText(0, 2, "e-mail");
			
			
			
			for (int row = 1; row < players.size(); ++row) {
				for (int column = 0; column < 2; ++column) {
					this.playersTable.setWidget(row, column, tableCellRenderer
							.render(row, column, players.get(row)));
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			Window.alert(ex.getMessage());
		}

	}

	public void setService(LeagueFoxServiceAsync leagueFoxService) {
		this.service = leagueFoxService;
	}

	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void setTableCellRenderer(TableCellRenderer<PlayerDto> tableCellRenderer) {
		this.tableCellRenderer = tableCellRenderer;
	}

	@Override
	public void setPresenter(
			com.leaguefox.web.client.view.PlayersView.Presenter<PlayerDto> presenter) {
		this.presenter = presenter;

	}

	@Override
	public void setRowData(List<PlayerDto> rowData) {
		updatePlayersTable(rowData);
	}

}
